<?php
/**
 * Created by PhpStorm.
 * User: 10467
 * Date: 2020/10/28
 * Time: 15:23
 */

namespace app\api\controller\Index;

use app\api\controller\AppController;
use app\api\model\User\UserModel;
use app\api\model\VideoUser\VideoUserCommentModel;
use app\api\model\VideoUser\VideoUserModel;
use app\Request;
use app\Service\CommentsService;
use app\Utils\Util;

use app\Utils\ImageUtil;
use think\facade\Db;

class UserComment extends AppController
{

    /**
     *评论列表
     */
    public function lists(Request $request)
    {
        $param = $request->param();
        $page = !empty($param['page']) ? $param['page'] : 1;
        $limit = !empty($param['limit']) ? $param['limit'] : 8;
        if (empty($param['video_id'])) {
            return Util::returnJson(self::CODE_ERROR, null, "视频ID必传");
        }
        $where = [
            ['video_id', '=', $param['video_id']],
            ['pid', '=', 0]
        ];
        $list = VideoUserCommentModel::getListWhere($page, $limit, $where);
        return Util::returnJson(self::CODE_SUCCESS, $list);
    }

    /**
     * 新增/回复评论
     */
    public function add(Request $request,CommentsService $service)
    {

        $data = [
            'uid' => $this->user['id']
        ];
        if (empty($data['uid'])) return Util::returnJson(self::CODE_ERROR, null, "参数错误");
        $data['video_id'] = (int)$request->param('video_id'); //视频ID
        if (empty($data['video_id'])) return Util::returnJson(self::CODE_ERROR, null, "视频ID必传");
        $data['content'] = $request->param('content'); //视频ID
        if (empty($data['content'])) return Util::returnJson(self::CODE_ERROR, null, "评论内容必传");
        $data['pid'] = (int)$request->param('comment_id'); //回复评论id
        if (empty($data['pid'])) $data['pid'] = 0;
        $id = VideoUserCommentModel::saves($data);
        if ($id) {

            $res =  $service->first($id);
            if($data['pid'] = 0)
            {
                Db::name('video_user')->where('id', $data['video_id'])->inc('nums_discuss', 1)->update();
            }
            return Util::returnJson(self::CODE_SUCCESS, $res, "评论成功");
        } else {
            return Util::returnJson(self::CODE_ERROR, null, "评论失败");
        }
    }

    /**
     *置顶/取消置顶评论
     */
    public function top(Request $request)
    {
        $param = $request->param();
        if (empty($this->user['id'])) return Util::returnJson(self::CODE_ERROR, null, "参数错误");
        if (empty($param['comment_id'])) return Util::returnJson(self::CODE_ERROR, null, "评论ID必传");
        if (!isset($param['top'])) return Util::returnJson(self::CODE_ERROR, null, "状态必传");
        $res = VideoUserCommentModel::top($param['comment_id'], $this->user['id'], $param['top']);
        if ($res) {
            return Util::returnJson(self::CODE_SUCCESS, null, "操作成功");
        } else {
            return Util::returnJson(self::CODE_ERROR, null, "操作失败");
        }
    }

    /**
     *删除评论
     */
    public function del(Request $request)
    {
        if (empty($this->user['id'])) return Util::returnJson(self::CODE_ERROR, null, "参数错误");
        $comment_id = $request->param('comment_id');
        if (empty($comment_id)) return Util::returnJson(self::CODE_ERROR, null, "评论ID必传");
        $where = [
            'uid' => $this->user['id'],
            'id' => $comment_id,
        ];
        $res = VideoUserCommentModel::dels($where);
        if ($res) {
            return Util::returnJson(self::CODE_SUCCESS, null, "删除成功");
        } else {
            return Util::returnJson(self::CODE_ERROR, null, "删除失败");
        }
    }

}